package com.siloam.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.siloam.module.Group;
import com.siloam.service.GroupService;
import com.siloam.vo.GroupVO;
import com.siloam.vo.UserVO;

@Controller
@RequestMapping("/web/group/group.do")
public class GroupController extends BaseController{
	
	@Resource
	private GroupService groupService;
	
	public GroupService getGroupService() {
		return groupService;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	@RequestMapping(params="method=list")
	@ResponseBody
	public List<GroupVO> list(){
		return groupService.findAll();
	}
	
	@RequestMapping(params="method=add")
	@ResponseBody
	public String add(Group group){
		groupService.add(group); 
		return SUCCESS;
	}
	
	@RequestMapping(params="method=initEdit")
	public String initEdit(int id, ModelMap modelMap){
		Group group = groupService.load(id);
		modelMap.put("group", group);
		return "editGroup";
	}
	
	@RequestMapping(params="method=update")
	@ResponseBody
	public String update(Group group){
		groupService.update(group); 
		return SUCCESS;
	}
	
	@RequestMapping(params="method=listGroupUser")
	@ResponseBody
	public List<UserVO> listGroupUser(int groupId){
		return groupService.listGroupUser(groupId);
	}
	
	@RequestMapping(params="method=setMemberType")
	@ResponseBody
	public String setMemberType(int type, int id){
		groupService.setMemberType(id, type);
		return SUCCESS;
	}
	
	@RequestMapping(params="method=delGroups")
	@ResponseBody
	public String deleteGroups(String ids){
		groupService.delete(ids);
		return SUCCESS;
	}
	
	@RequestMapping(params="method=listByType")
	@ResponseBody
	public List<Group> listByType(){
		return groupService.findByType(Group.TYPE_USER);
	}
	
	@RequestMapping(params="method=addGroupUsers")
	@ResponseBody
	public String addGroupUsers(String ids, int groupId){
		groupService.addGroupUsers(ids, groupId);
		return SUCCESS;
	}
	
	@RequestMapping(params="method=deleteGroupUsers")
	@ResponseBody
	public String deleteGroupUsers(String ids){
		groupService.deleteGroupUsers(ids);
		return SUCCESS;
	}
	
}
